﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Accord.Imaging;
using System.Xml.Serialization;
using System.IO;
using System.Globalization;
using Accord.Vision.Detection;
using Accord.Vision;

namespace Face_Recognition.Haar
{
    public class Cascade
    {
        private int initialWidth;

        public int InitialWidth
        {
            get { return initialWidth; }
            set { initialWidth = value; }
        }

        private int initialHeight;

        public int InitialHeight
        {
            get { return initialHeight; }
            set { initialHeight = value; }
        }

        public Cascade(int _initalWidth, int _initialHeight)
        {
            InitialHeight = _initialHeight;
            InitialWidth = _initalWidth;
        }

        public Stage[] Stages { get; protected set; }

        public Cascade(int baseWidth, int baseHeight, Stage[] stages)
        {
            initialWidth = baseWidth;
            initialHeight = baseHeight;
            Stages = stages;
        }

        public Cascade(int baseWidth, int baseHeight, HaarCascadeStage[] stages)
        {
            initialWidth = baseWidth;
            initialHeight = baseHeight;
            //Stages = stages;
        }

        //public object Clone()
        //{
        //    Stage[] newStages = new Stage[Stages.Length];
        //    for (int i = 0; i < newStages.Length; i++)
        //        newStages[i] = (Stage)Stages[i].Clone();
        //    Cascade r = new Cascade(initialWidth, initialHeight);
        //    r.Stages = newStages;

        //    return r;
        //}

        public Cascade(string xmlLocation)
        {
            XmlTextReader xmlReader = new XmlTextReader(new StreamReader(xmlLocation));

            // Gathers the base window size
            xmlReader.ReadToFollowing("size");
            string size = xmlReader.ReadElementContentAsString();

            // Proceeds to load the cascade stages
            xmlReader.ReadToFollowing("stages");
            XmlSerializer serializer = new XmlSerializer(typeof(HaarCascadeSerializationObject));
            var stages = (HaarCascadeSerializationObject)serializer.Deserialize(xmlReader);

            // Process base window size
            string[] s = size.Trim().Split(' ');
            int baseWidth = int.Parse(s[0], CultureInfo.InvariantCulture);
            int baseHeight = int.Parse(s[1], CultureInfo.InvariantCulture);

            // Fill cascade with values
            this.InitialHeight = baseHeight;
            this.InitialWidth = baseWidth;

            this.Stages = new Stage[stages.Stages.Length];

            int i = 0;
            foreach (HaarCascadeStage hs in stages.Stages)
            {
                this.Stages[i].NextIndex = hs.NextIndex;
                this.Stages[i].ParentIndex = hs.ParentIndex;
                this.Stages[i].setTrees(hs.Trees);
                this.Stages[i].Threshold = hs.Threshold;
            }
            //this.Stages = stages.Stages;
            //return null;

        }
    }
}
